package org.study.mybatisplus.dynamic.boot.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.study.mybatisplus.dynamic.boot.config.SystemConstant;
import org.study.mybatisplus.dynamic.boot.dto.CreateDeviceDTO;
import org.study.mybatisplus.dynamic.boot.model.IotDatasource;
import org.study.mybatisplus.dynamic.boot.model.IotLog;
import org.study.mybatisplus.dynamic.boot.vo.DataValueVO;

import java.util.*;

/**
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年10月08日
 */
@Mapper
public interface IotDatasourceMapper extends BaseMapper<IotDatasource> {
    List<String> showTables();

    @DS(SystemConstant.DS_XUGU)
    List<String> showTables4Xugu();

    void executeSql(@Param("sql") String sql);

    void insertSql(@Param("sql") String sql);

    void dropTable(@Param("tableName") String tableName);

    void createTable(@Param("tableName") String tableName,
                     @Param("gatewayNo") String gatewayNo,
                     @Param("deviceNo") String deviceNo);

    void createTableWithFiled(@Param("tableName") String tableName,
                              @Param("gatewayNo") String gatewayNo,
                              @Param("deviceNo") String deviceNo,
                              @Param("insertList") List<CreateDeviceDTO> insertList);

    @DS(SystemConstant.DS_TDENGINE)
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    void createLogTable(@Param("tableName") String tableName,
                        @Param("comment") String comment);

    @DS(SystemConstant.DS_TDENGINE)
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    void createPropertyTable(@Param("tableName") String tableName,
                             @Param("comment") String comment);

    void addColumn(@Param("tableName") String tableName,
                   @Param("columnName") String columnName,
                   @Param("columnType") String columnType);

    void addColumnNchar(@Param("tableName") String tableName,
                        @Param("columnName") String columnName,
                        @Param("columnType") String columnType,
                        @Param("propertyScale") Integer propertyScale);

    void addColumnDecimal(@Param("tableName") String tableName,
                          @Param("columnName") String columnName,
                          @Param("columnType") String columnType,
                          @Param("precision") Integer precision,
                          @Param("scale") Integer scale);

    void dropColumn(@Param("tableName") String tableName,
                    @Param("columnName") String columnName);

    void modifyColumn(@Param("tableName") String tableName,
                      @Param("oldColumnName") String oldColumnName,
                      @Param("newColumnName") String newColumnName);

    @DS(SystemConstant.DS_TDENGINE)
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    void insertDataMap(@Param("tableName") String tableName,
                       @Param("time") String time,
                       @Param("gatewayNo") String gatewayNo,
                       @Param("deviceNo") String deviceNo,
                       @Param("insertMap") Map<String, Object> insertMap);

    void insertLog(@Param("tableName") String tableName,
                   @Param("time") Object time,
                   @Param("gatewayNo") Object gatewayNo,
                   @Param("deviceNo") Object deviceNo,
                   @Param("content") Object content,
                   @Param("id") Object id);

    void insertLogMap(@Param("tableName") String tableName,
                      @Param("insertMap") Map<String, Object> insertMap);

    Map<String, Object> selectCurrentData(@Param("tableName") String tableName,
                                          @Param("propertyNoList") List<String> propertyNoList);

    IPage<LinkedHashMap<String, Object>> selectHistoryData(Page<LinkedHashMap<String, Object>> page,
                                                           @Param("tableName") String tableName,
                                                           @Param("propertyNoList") List<String> propertyNoList,
                                                           @Param("startTime") String startTime,
                                                           @Param("endTime") String endTime, @Param("sort") String sort);


    IPage<IotLog> selectLogList(Page<IotLog> page,
                                @Param("tableName") String tableName,
                                @Param("startTime") String startTime,
                                @Param("endTime") String endTime);

    @DS(SystemConstant.DS_TDENGINE)
    List<LinkedHashMap<String, Object>> parseListMap4TDengine(@Param("sql") String sql);

    @DS(SystemConstant.DS_TDENGINE)
    LinkedHashMap<String, Object> parseMap4TDengine(@Param("sql") String sql);

    @DS(SystemConstant.DS_TDENGINE)
    List<DataValueVO> parseBean4TDengine(@Param("sql") String sql);
}
